#!/bin/bash
###############################
#
#功能：拉服务器上其他人已经push的分支
#执行脚本前提：repo status 查看，没有未提交的内容，所有分支都在公版分支上面
#拷贝ly_repo到.repo 所在的目录
#sce_dir 为要参考的代码目录，默认为：/media/sdc/ly/x_s905x，只是为了获取分支所在的所有git目录；
#一般在软件工作记录里面会有分支目录，可以直接拷贝分支目录到 $branch_name.txt文件中，删除该脚本 tag0 ~ tag1 中间的部分内容；
#AMH为公版分支，默认为m-amlogic
#branch_name 为要拉取的分支名,默认为netxeon_0515
#
#  made by ly
###############################


repo branch
sce_dir=/media/sdc/ly/x_s905x
cur_dir=`pwd`
branch_name="netxeon_0701"
NBH="no branch"
AMH="m-amlogic"



########## tag0 #######################


cd $sce_dir
repo branch > $cur_dir/branch.txt
cd -
grep -n $branch_name ./branch.txt > tmp
startl=`awk -F ':' '{print $1}' tmp`
sed -i "1,$startl d" branch.txt
#sed -n '/netxeon_0515/p' ./branch.txt

sed -i '/| in/,$ d' branch.txt
#grep -n "| in" ./branch.txt > tmp
#startl=`awk -F ':' '{print $1}' tmp`
#echo $startl
#sed -i "$startl,$ d" branch.txt
#cat branch.txt
gawk '{print $1}' branch.txt > tmp
mv tmp $branch_name.txt
#cat $branch_name.txt
#rm branch.txt

########  tag1  ###############################

for eachdir in `cat $branch_name.txt`
do
#echo $eachdir
#echo mark
cd $eachdir
git branch > $cur_dir/tmp 
NETH=$branch_name
#grep "no branch" tmp > tmp1
#flagnobranch=`cat tmp1`
tmp1=`grep "$NBH" $cur_dir/tmp`
tmp2=`grep "$AMH" $cur_dir/tmp`
tmp3=`grep "$NETH" $cur_dir/tmp`
#echo $flagnobranch
#num=`echo ${#flagnobranch}`
#if [ $num -gt 0 ]

if [ -z "$tmp1" ]
then
   echo aaa
	if [ -n "$tmp3" ]
	then
		git branch -D $NETH
	fi
	git checkout -b $NETH
	git remote update
	git pull openlinux $NETH
else
    echo nobranch
	if [ -n "$tmp2" ]
	then
		git branch -D $AMH
	fi
	if [ -n "$tmp3" ]
	then
		git branch -D $NETH
	fi
	git checkout -b $AMH
	git checkout -b $NETH
	git remote update
	git pull openlinux $NETH
fi
cd -
#rm tmp 
#rm tmp1
done
rm branch.txt
cat $branch_name.txt
repo branch





